<?php
namespace app\content\controller;

use app\model\Classes;
use app\model\ClassPlan as ModelClassPlan;
use app\model\ClassTaskComment as ModelClassTaskComment;
use app\model\ClassUser;
use app\model\Product;
use app\model\User;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 打卡记录
 * @class ClassTaskComment
 * @package app\content\controller
 */
class ClassTaskComment extends Controller
{
  
    /**
     * 打卡记录
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        ModelClassTaskComment::mQuery()->layTable(function () {
            $this->title = '打卡记录';
            $this->class_list = Classes::mk()->where(['status' => 1])->select();
            $this->product_list = Product::mk()->where(['status' => 1])->select();
        }, function (QueryHelper $query) {

            // 加载对应数据列表
            // $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]);

            // 关联用户身份资料
            /** @var \think\model\Relation|\think\db\Query $query */
            $query->with(['user' => static function ($query) {
                $query->field('id,name,sex,full_name,phone,image_path');
            }, 'classes' => static function ($query) {
                $query->field('id,name');
            }]);

            // 数据列表搜索过滤
            $query->equal('parent_id')->equal('product_id')->dateBetween('start_date');
            $query->like('name');
        });
    }

    protected function _index_page_filter(&$data){
        foreach ($data as $key => $value) {
            if(!empty($value['images'])){
                $data[$key]['images'] = explode(',',$value['images']);
            }
        }
    }

    /**
     * 添加
     * @auth true
     */
    public function add()
    {
        $this->product_list = Product::mk()->where(['status' => 1])->select();
        ModelClassTaskComment::mForm('form');
    }

    /**
     * 编辑
     * @auth true
     */
    public function edit()
    {
   
        $this->product_list = Product::mk()->where(['status' => 1])->select();
        ModelClassTaskComment::mForm('form');
    }


    public function edit_form_filter(array &$data){
        
    }

    /**
     * 修改信息
     * @auth true
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            ModelClassTaskComment::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
             
            ]);
            $ModelClassPlan = ModelClassTaskComment::mk()->findOrEmpty($data['id']);
            if ($ModelClassPlan->isExists() && $ModelClassPlan->save([])) {
             
                $this->success('');
            } else {
                $this->error('');
            }
        }
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        
    }

    /**
     * 修改状态
     * @auth true
     */
    public function state()
    {
        $this->_checkInput();
        ModelClassTaskComment::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除
     * @auth true
     */
    public function remove()
    {
        $this->_checkInput();
        ModelClassTaskComment::mDelete();
    }

    /**
     * 检查输入变量
     */
    private function _checkInput()
    {
       
    }

}
